﻿using System;
using System.Collections.Generic;
using System.Text;
using DreamShield.Deployment.Assistants.DreamWizard.WizardInterfaces.StandardWizard;
using DreamShield.Deployment.Assistants.DreamWizard;
using DreamShield.Deployment.Installers.DreamWorker;
using DreamShield.Deployment.Wizards.Workflow;
using DreamShield.Deployment.Installers.DreamWorker.PluginsInterfaces;

namespace DreamShield.Deployment.Miles.Events
{
    /// <summary>
    /// Classe offrant la possibilité d'intercepter les évènements
    /// du wizard standard d'installation
    /// </summary>
    public class EventsProvider_StdWizard : BaseMilesPlugin
    {
        /// <summary>
        /// Construit une nouvelle instance du fournisseur d'évènements du package
        /// </summary>
        /// <param name="server">Serveur Miles en cours</param>
        public EventsProvider_StdWizard(MilesRootServer server)
            : base(server)
        {
            NativePluginInstance = new NativePlugin(this);

            _NativePluginImplementation = NativePluginInstance;
            _NativePluginType = StandardWizardProvider.StandardWizardProvider_PluginsGuid;
        }

        #region Liason avec DreamShield

        /// <summary>
        /// Instance native de plugin DreamShield
        /// </summary>
        protected virtual NativePlugin NativePluginInstance { get; set; }

        /// <summary>
        /// Classe de liaison avec le runtime DreamShield
        /// </summary>
        protected class NativePlugin : BaseStandardWizardProvider_Plugin
        {
            /// <summary>
            /// Fournisseur parent
            /// </summary>
            public virtual EventsProvider_StdWizard Owner { get; protected set; }

            /// <summary>
            /// Crée une nouvelle classe de liaison avec le runtime DreamShield
            /// </summary>
            /// <param name="owner">Fournisseur parent</param>
            public NativePlugin(EventsProvider_StdWizard owner)
            {
                Owner = owner;
            }

            /// <summary>
            /// Infrastructure
            /// </summary>
            /// <param name="engine"></param>
            /// <param name="actionId"></param>
            /// <param name="worker"></param>
            public override void WizardWorkerExecute_Begin(DreamWizardEngine engine, WorkerActionId actionId, IDreamWizardWorker worker)
            {
                Owner.WizardWorkerExecute_Begin(engine, actionId);
            }

            /// <summary>
            /// Infrastructure
            /// </summary>
            /// <param name="engine"></param>
            /// <param name="actionId"></param>
            /// <param name="worker"></param>
            public override void WizardWorkerExecute_End(DreamWizardEngine engine, WorkerActionId actionId, IDreamWizardWorker worker)
            {
                Owner.WizardWorkerExecute_End(engine, actionId);
            }
        }
        #endregion

        /// <summary>
        /// Evènement lors d'une action de l'assistant standard
        /// </summary>
        /// <param name="engine">Engine actuel</param>
        /// <param name="actionId">Action effectuée par l'assistant</param>
        public delegate void WizardWorker_Event(DreamWizardEngine engine, WorkerActionId actionId);

        /// <summary>
        /// Appelé avant le début de l'exécution de l'assistant
        /// </summary>
        public event WizardWorker_Event WizardWorkerExecute_BeginEvent;

        /// <summary>
        /// Appelé après l'exécution de l'assistant
        /// </summary>
        public event WizardWorker_Event WizardWorkerExecute_EndEvent;

        /// <summary>
        /// Déclanche l'évènement WizardWorkerExecute_BeginEvent
        /// </summary>
        /// <param name="engine">Engine actuel</param>
        /// <param name="actionId">Action effectuée par l'assistant</param>
        protected virtual void WizardWorkerExecute_Begin(DreamWizardEngine engine, WorkerActionId actionId)
        {
            if (WizardWorkerExecute_BeginEvent != null)
                WizardWorkerExecute_BeginEvent(engine, actionId);
        }

        /// <summary>
        /// Déclanche l'évènement WizardWorkerExecute_EndEvent
        /// </summary>
        /// <param name="engine">Engine actuel</param>
        /// <param name="actionId">Action effectuée par l'assistant</param>
        protected virtual void WizardWorkerExecute_End(DreamWizardEngine engine, WorkerActionId actionId)
        {
            if (WizardWorkerExecute_EndEvent != null)
                WizardWorkerExecute_EndEvent(engine, actionId);
        }
    }
}
